package com.ovstech.samples.axon.file;

import com.ovstech.samples.axon.file.command.CreateFileNodeCommand;
import com.ovstech.samples.axon.file.command.UpdateFileNodeCommand;
import lombok.Data;
import org.axonframework.commandhandling.CommandHandler;
import org.axonframework.commandhandling.model.AggregateIdentifier;
import org.axonframework.eventhandling.EventHandler;
import org.axonframework.spring.stereotype.Aggregate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.axonframework.commandhandling.model.AggregateLifecycle.apply;

@Data
@Aggregate
public class FileNode {

    private static final Logger logger = LoggerFactory.getLogger(FileNode.class);

    @AggregateIdentifier
    private String id;

    private String name;

    private Long size;

    @CommandHandler
    public FileNode(CreateFileNodeCommand command){
        logger.info("in FileNode()");
        apply(new FileNodeCreatedEvent(command.getFileNodeId(),command.getName(), command.getSize()));
    }

    @CommandHandler
    public void doUpdate(UpdateFileNodeCommand command){
        logger.info("doUpdate");
        apply(new FileNodeUpdatedEvent(command.getFileNodeId(), command.getName()));
    }

    @EventHandler
    public void onCreated(FileNodeCreatedEvent event){
        logger.info("onCreated");
        this.id = event.getFileNodeId();
        this.name = event.getName();
        this.size = event.getSize();
    }

    @EventHandler
    public void onUpdated(FileNodeUpdatedEvent event){
        logger.info("onUpdated");
        this.name = event.getName();
    }

}
